<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">用户名称：</span>
                <el-input placeholder="输入用户名称" v-model.trim="nickname" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">用户UID：</span>
                <el-input placeholder="输入用户UID" v-model.trim="user_id" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">变动来源：</span>
                <el-select v-model="froms" clearable placeholder="请选择变动来源">
                    <el-option label="余额" value="money" />
                    <el-option label="分佣奖励" value="award" />
                    <el-option label="微信" value="weixin" />
                    <el-option label="授信额" value="credit" />
                    <el-option label="会员成长值" value="exp_point" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">变动类型：</span>
                <el-select v-model="types" clearable placeholder="请选择类型">
                    <el-option label="全部" value="0" />
                    <el-option label="消费" value="1" />
                    <el-option label="收益" value="2" />
                    <el-option label="提现" value="3" />
                    <el-option label="充值" value="4" />
                    <el-option label="退款" value="5" />
                    <el-option label="欠款" value="6" />
                    <el-option label="扣款" value="7" />
                    <el-option label="平台调整" value="8" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">时间区间：</span>
                <el-date-picker
                    v-model="area_time"
                    type="daterange"
                    unlink-panels
                    value-format="YYYY-MM-DD"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                />
            </div>
            <div class="task-one__btn task-wrap-btn clearfix">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">搜索</el-button>
                <el-button @click="clickExport">导出</el-button>
            </div>
        </div>

        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
        >
            <el-table-column label="昵称/ID" min-width="140">
                <template #default="{ row }">
                    <p>{{ row.nickname }}</p>
                    <p>ID:{{ row.uid }}</p>
                </template>
            </el-table-column>
            <el-table-column label="姓名/电话" min-width="140">
                <template #default="{ row }">
                    <p>{{ row.name }}</p>
                    <p>{{ row.mobile }}</p>
                </template>
            </el-table-column>
            <el-table-column label="变动来源" min-width="120">
                <template #default="{ row }">{{ from_label[row.from].text}}</template>
            </el-table-column>
            <el-table-column label="变动类型" min-width="120">
                <template #default="{ row }">{{ type_label[row.type] }}</template>
            </el-table-column>
            <el-table-column prop="number" label="调整金额(元)" min-width="120"></el-table-column>
            <el-table-column prop="intro" label="调整备注" min-width="280"></el-table-column>
            <el-table-column prop="created_at_label" label="创建时间" width="170" fixed="right"></el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
    </div>
    <go-back />
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch } from "vue"
import { Search } from "@element-plus/icons-vue"
import OneLoad from "@mixins/one-load"
import GoBack from "@components/GoBack.vue"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getList()
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, search_btn, pubParams, pubRefreshParams, getdata } = OneLoad()
// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    pubParams(getList)
})

const type_label = ["消费", "收益", "提现", "充值", "退款", "欠款", "扣款", "平台调整"]
const from_label = {
    money: {
        text: "余额",
    },
    award: {
        text: "分佣奖励",
    },
    weixin: {
        text: "微信",
    },
    credit: {
        text: "授信额",
    },
    exp_point: {
        text: "会员成长值",
    },
    allinpay: {
        text: "通联支付",
    },
}
function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
        uid: user_id.value,
        so: nickname.value,
        from: froms.value,
        type: types.value,
        s_time: area_time.value ? area_time.value[0] : "",
        l_time: area_time.value ? area_time.value[1] : "",
    }
    getdata("finance/userLog", params)
}

/**
 * 搜索
 * @param {String} nickname 会员名称
 * @param {String} user_id 会员ID
 * @param {String} area_time 注册时间段
 * @param {String} froms 来源 [money:余额,award:分佣奖励,weixin:微信,credit:授信额,exp_point:会员成长值]
 * @param {String} types 类型 [0:全部,1:消费,2:收益,3:提现,4:充值,5:退款,6:欠款,7:扣款,8:平台调整]
 */
const nickname = ref("")
const user_id = ref("")
const area_time = ref("")
const froms = ref("")
const types = ref("")
const handleSearch = () => {
    search_btn.value = true
    pubRefreshParams(getList)
}

// 导出 
const clickExport = () => {
    proxy.$axios
        .get("finance/userLogExport", {
            params: {
                uid: user_id.value,
                so: nickname.value,
                from: froms.value,
                type: types.value,
                s_time: area_time.value ? area_time.value[0] : "",
                l_time: area_time.value ? area_time.value[1] : "",
            },
            responseType: "blob",
        })
        .then(function (res) {
            let contentDisposition = res.headers["content-disposition"]
            let filename = decodeURI(contentDisposition.split("fileName=")[1] || contentDisposition.split("filename=")[1])
            let blob = new Blob([res.data], { type: res.headers["content-type"] })
            const url = window.URL.createObjectURL(blob)
            const link = document.createElement("a")
            link.href = url
            link.download = filename
            document.body.appendChild(link)
            link.click()
            URL.revokeObjectURL(url) // 释放内存
            document.body.removeChild(link)
        })
        .catch((err) => {})
}
</script>
<script>
export default {
    name: "CapitalLog",
}
</script>

<style lang="less" scoped></style>
